﻿#region License
/*
Copyright (c) 2005-2011, CellAO Team

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the CellAO Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Usings...
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using AO.Core;
using Config = AO.Core.Config.ConfigReadWrite;
using MySql.Data.MySqlClient;
using Cell.Core;
using NLog;
using NLog.Config;
using NLog.Targets;
using ZoneEngine.Script;
//using NLog.Win32.Targets;
#endregion

namespace ZoneEngine
{
    public class Program
    {
        public static ScriptAPI Script;
        public static Server zoneServer;

        public static Functions.FunctionCollection FunctionC = new Functions.FunctionCollection();

        static void Main(string[] args)
        {
            bool processedargs = false;

            // Please dont kill the commented out lines below for the moment -NV
            //Misc.Playfields.Instance.playfields[0].districts.Add(new ZoneEngine.Misc.DistrictInfo());
            //Misc.Playfields.Instance.playfields[0].districts[0].districtName = "some district";
            //Misc.Playfields.Instance.playfields[0].districts.Add(new ZoneEngine.Misc.DistrictInfo());
            //Misc.Playfields.Instance.playfields[0].districts[1].districtName = "some other district";
            //Misc.DistrictInfo.DumpXML(@"C:\list.xml", Misc.Playfields.Instance.playfields[0]);

            #region Console Text...

            Console.Title = "CellAO " + AssemblyInfoclass.Title + " Console. Version: " + AssemblyInfoclass.Description + " " + AssemblyInfoclass.AssemblyVersion;
            ConsoleText ct = new ConsoleText();
            ct.TextRead("main.txt");
            Console.WriteLine("Loading " + AssemblyInfoclass.Title + "...");
            if (ismodified())
            {
                Console.WriteLine("Your " + AssemblyInfoclass.Title + " was compiled from modified source code.");
            }
            else
                if (ismixed())
                {
                    Console.WriteLine("Your " + AssemblyInfoclass.Title + " uses mixed SVN revisions.");
                }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Using ISComm v1.0");
            Console.WriteLine("[OK]");
            Console.ResetColor();
            #endregion

            #region ISComm Code Area...

            Console.WriteLine("[ISComm] Waiting for Link...");
            ChatCom.StartLink(Config.Instance.CurrentConfig.CommPort);
            //System.Console.WriteLine("[ISComm] Linked Successfully! :D");

            #endregion

            zoneServer = new Server { EnableTCP = true, EnableUDP = false };

            #region Script Loading Code Area..
            CScriptCompiler csc = new CScriptCompiler();
            #endregion

            try { zoneServer.TcpIP = IPAddress.Parse(Config.Instance.CurrentConfig.ListenIP); }
            catch { ct.TextRead("ip_config_pharse_error.txt"); Console.ReadKey(); return; }
            try { zoneServer.UdpIP = IPAddress.Parse(Config.Instance.CurrentConfig.ListenIP); }
            catch { ct.TextRead("ip_config_pharse_error.txt"); Console.ReadKey(); return; }

            zoneServer.TcpPort = Convert.ToInt32(Config.Instance.CurrentConfig.ZonePort);

            #region NLog

            LoggingConfiguration config = new LoggingConfiguration();
            ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget();
            consoleTarget.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
            FileTarget fileTarget = new FileTarget();
            config.AddTarget("file", fileTarget);
            fileTarget.FileName = "${basedir}/ZoneEngineLog.txt";
            fileTarget.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
            LoggingRule rule1 = new LoggingRule("*", LogLevel.Trace, consoleTarget);
            config.LoggingRules.Add(rule1);
            LoggingRule rule2 = new LoggingRule("*", LogLevel.Trace, fileTarget);
            config.LoggingRules.Add(rule2);
            LogManager.Configuration = config;

            #endregion


            FunctionC.ReadFunctions();
            Console.WriteLine("Registered " + FunctionC.NumberofRegisteredFunctions().ToString() + " Functions");

            #region Console Commands...
            string consoleCommand;
            ct.TextRead("zone_consolecommands.txt");
            // removed CheckDBs here, added commands check and updatedb (updatedb will change to a versioning 
            //            lintels();
            while (true)
            {
                if (!processedargs)
                {
                    if (args.Length == 1)
                    {
                        if (args[0].ToLower() == "/autostart")
                        {
                            ct.TextRead("autostart.txt");
                            csc.Compile(false);
                            StartTheServer();
                        }
                    }
                    processedargs = true;
                }
                Console.Write("\nServer Command >>");
                consoleCommand = Console.ReadLine();
                switch (consoleCommand.ToLower())
                {
                    case "start":
                        if (zoneServer.Running)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("Zone Server is already running");
                            Console.ResetColor();
                            break;
                        }


                        //TODO: Add SQL Check.
                        csc.Compile(false);
                        StartTheServer();
                        break;
                    case "startm": // Multiple dll compile
                        if (zoneServer.Running)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("Zone Server is already running");
                            Console.ResetColor();
                            break;
                        }


                        //TODO: Add SQL Check.
                        csc.Compile(true);
                        StartTheServer();
                        break;
                    case "stop":
                        if (!zoneServer.Running)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("Zone Server is not running");
                            Console.ResetColor();
                            break;
                        }
                        zoneServer.Stop();
                        ThreadMgr.Stop();
                        break;
                    case "check":
                    case "updatedb":
                        CheckDBs();
                        break;
                    case "exit":
                    case "quit":
                        if (zoneServer.Running)
                        { zoneServer.Stop(); ThreadMgr.Stop(); }
                        Process.GetCurrentProcess().Kill();
                        break;
                    case "ls": //list all available scripts, dont remove it since it does what it should
                        Console.WriteLine("Available scripts");
                        /* Old Lua way
                        string[] files = Directory.GetFiles("Scripts");*/
                        string[] files = Directory.GetFiles("Scripts\\", "*.cs", SearchOption.AllDirectories);
                        if (files.Length == 0)
                        {
                            Console.WriteLine("No scripts were found.");
                            break;
                        }
                        Console.ForegroundColor = ConsoleColor.Green;
                        foreach (string s in files)
                        {
                            Console.WriteLine(s);
                            /* Old Lua way
                            if (s.EndsWith(".lua"))
                            {
                                Console.WriteLine(s.Split('\\')[1].Split('.')[0]);
                            }*/
                        }
                        Console.ResetColor();
                        break;
                    case "ping":
                        // ChatCom.Server.Ping();
                        Console.WriteLine("Ping is disabled till we can fix it");
                        break;
                    case "running":
                        if (zoneServer.Running)
                        {
                            Console.WriteLine("Zone Server is Running");
                            break;
                        }
                        Console.WriteLine("Zone Server not Running");
                        break;
                    default:
                        ct.TextRead("zone_consolecmdsdefault.txt");
                        break;
                }
            }
        }

            #endregion

        // This needs to be rewritten for the new database system..
        // *Winks at Algorithman*
        #region Check Database
        public static void CheckDBs()
        {
            SqlWrapper ms = new SqlWrapper();
            List<string> tablelist = new List<string>();
            List<string> tabletodo = new List<string>();
            bool allok = true;
            string sqltype = Config.Instance.CurrentConfig.SQLType;
            // ToDo: check if database exists and create it if not (parsing the connection string)
            if (sqltype == "MsSql")
            {
                ms.SqlRead("SELECT table_name FROM INFORMATION_SCHEMA.TABLES;");
            }
            else
                if (sqltype == "PostgreSQL")
                {
                    ms.SqlRead("SELECT table_name FROM information_schema.tables;");
                }
                else
                    if (sqltype == "MySql")
                    {
                        ms.SqlRead("show Tables");
                    }
            if (ms.myreader.HasRows)
            {
                while (ms.myreader.Read())
                {
                    tablelist.Add(ms.myreader.GetString(0));
                }
            }
            else
            {
                allok = false;
            }
            ms.sqlclose();

            string[] sqlfiles = Directory.GetFiles("SQLTables");
            bool isin;
            foreach (string s in sqlfiles)
            {
                isin = false;
                foreach (string table in tablelist)
                {
                    if (s.ToLower() == System.IO.Path.Combine("SQLTables", table + ".sql").ToLower())
                    {
                        isin = true;
                        break;
                    }
                }
                if (!isin)
                {
                    tabletodo.Add(s);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Table " + s + " doesn't exist.");
                    allok = false;
                }
            }

            if (!allok)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("SQL Tables are not complete. Should they be created? (Y/N) ");
                string answer = Console.ReadLine();
                string sqlquery;
                if (answer.ToLower() == "y")
                {
                    foreach (string todo in tabletodo)
                    {
                        long filesize = new FileInfo(todo).Length;
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write("Table " + todo.PadRight(67) + "[  0%]");

                        if (filesize > 10000)
                        {
                            string[] queries = File.ReadAllLines(todo);
                            int c = 0;
                            sqlquery = "";
                            string lastpercent = "0";
                            while (c < queries.Length)
                            {
                                if (queries[c].IndexOf("INSERT INTO") == -1)
                                {
                                    sqlquery += queries[c] + "\n";
                                }
                                else
                                {
                                    c--;
                                    break;
                                }
                                c++;
                            }
                            ms.SqlInsert(sqlquery);
                            c++;
                            while (c < queries.Length)
                            {
                                ms.SqlInsert(queries[c++]);
                                string lp = Convert.ToInt32(Math.Floor((double)c / queries.Length * 100)).ToString();
                                if (lp != lastpercent)
                                {
                                    Console.Write("\rTable " + todo.PadRight(67) + "[" + lp.PadLeft(3) + "%]");
                                    lastpercent = lp;
                                }
                            }
                        }
                        else
                        {
                            sqlquery = File.ReadAllText(todo);
                            ms.SqlInsert(sqlquery);
                            Console.Write("\rTable " + todo.PadRight(67) + "[100%]");
                        }
                        Console.WriteLine();
                    }
                }

            }
            Console.ForegroundColor = ConsoleColor.Green;
            if (sqltype == "MySql")
            {
                Console.Write("Optimizing tables ");
                foreach (string table in tablelist)
                {
                    ms.SqlInsert("OPTIMIZE TABLE " + table);
                    Console.Write(table + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("Database is fine.");
            Console.ResetColor();
        }
        #endregion

        public class lintel
        {
            public int ID;
            public Single X;
            public Single Y;
            public Single Z;
            public Single HZ;
            public int frompf;
            public int topf;
            public int toid;
            public int proxy;
        }

        public static void lintels()
        {
            SqlWrapper ms = new SqlWrapper();
            List<lintel> doors = new List<lintel>();
            lintel ll;
            ms.SqlRead("SELECT * FROM doors");
            #region MySql
            if (ms.ismysql)
            {
                while (ms.myreader.Read())
                {
                    ll = new lintel();
                    ll.ID = ms.myreader.GetInt32("ID");
                    ll.X = ms.myreader.GetFloat("X");
                    ll.Y = ms.myreader.GetFloat("Y");
                    ll.Z = ms.myreader.GetFloat("Z");
                    ll.HZ = ms.myreader.GetFloat("HZ");
                    ll.frompf = ms.myreader.GetInt32("playfield");
                    ll.topf = ms.myreader.GetInt32("toplayfield");
                    ll.toid = ms.myreader.GetInt32("toid");
                    ll.proxy = ms.myreader.GetInt32("proxy");
                    doors.Add(ll);
                }
                ms.myreader.Close();
                ms.mcc.Close();
                ms.mcc.Dispose();
            }
            #endregion
            #region MsSql
            if (ms.ismssql)
            {
                while (ms.sqlreader.Read())
                {
                    ll = new lintel();
                    ll.ID = ms.sqlreader.GetInt32(0);
                    ll.X = ms.sqlreader.GetFloat(1);
                    ll.Y = ms.sqlreader.GetFloat(2);
                    ll.Z = ms.sqlreader.GetFloat(3);
                    ll.HZ = ms.sqlreader.GetFloat(4);
                    ll.frompf = ms.sqlreader.GetInt32(5);
                    ll.topf = ms.sqlreader.GetInt32(6);
                    ll.toid = ms.sqlreader.GetInt32(7);
                    ll.proxy = ms.sqlreader.GetInt32(8);
                    doors.Add(ll);
                }
                ms.sqlreader.Close();
                ms.sqlcc.Close();
                ms.sqlcc.Dispose();
            }
            #endregion
            #region PostgreSQL
            if (ms.isnpgsql)
            {
                while (ms.npgreader.Read())
                {
                    ll = new lintel();
                    ll.ID = ms.npgreader.GetInt32(0);
                    ll.X = ms.npgreader.GetFloat(1);
                    ll.Y = ms.npgreader.GetFloat(2);
                    ll.Z = ms.npgreader.GetFloat(3);
                    ll.HZ = ms.npgreader.GetFloat(4);
                    ll.frompf = ms.npgreader.GetInt32(5);
                    ll.topf = ms.npgreader.GetInt32(6);
                    ll.toid = ms.npgreader.GetInt32(7);
                    ll.proxy = ms.npgreader.GetInt32(8);
                    doors.Add(ll);
                }
                ms.npgreader.Close();
                ms.npgcc.Close();
                ms.npgcc.Dispose();
            }
            #endregion
            bool found;
            foreach (lintel l1 in doors)
            {
                found = false;
                foreach (lintel l2 in doors)
                {
                    if (l1.ID != l2.ID)
                    {
                        if (l1.topf != 0)
                        {
                            if ((l1.frompf == l2.topf) && (l1.topf == l2.frompf))
                            {
                                found = true;
                                l1.toid = l2.ID;
                                ms.SqlUpdate("UPDATE doors set toid=" + l2.ID.ToString() + " where id=" + l1.ID.ToString());
                                Console.WriteLine(l1.ID.ToString());
                            }
                            else
                                if ((l1.topf == l2.frompf) && (l2.topf == 0))
                                {
                                    l1.toid = l2.ID;
                                    ms.SqlUpdate("UPDATE doors set toid=" + l2.ID.ToString() + " where id=" + l1.ID.ToString());
                                    Console.WriteLine(l1.ID.ToString());
                                }
                        }
                    }
                }
                if (!found)
                {
                    ms.SqlUpdate("UPDATE doors SET proxy=1 where ID=" + l1.ID.ToString());
                }
            }
        }

        public static void StartTheServer()
        {
            try
            {
                Console.ForegroundColor = ConsoleColor.Green;
                zoneServer.Monsters = new List<NonPC>();
                zoneServer.Vendors = new List<VendingMachine>();
                zoneServer.Doors = new List<Doors>();
                Console.WriteLine("Loaded {0} spawns", NPC.NPCHandler.CacheAllFromDB());
                Console.WriteLine("Loaded {0} vendors", NPC.VendorHandler.CacheAllFromDB());
                Console.WriteLine("Loaded {0} teleports", Misc.DoorHandler.CacheAllFromDB());
                Console.WriteLine("Loaded {0} statels", Statels.CacheAllStatels());
                ThreadMgr.Start();
                zoneServer.Start();
                Console.ResetColor();
            }
            catch (MySqlException e)
            {
                Console.WriteLine("MySQL Error. Server Cannot Start");
                Console.WriteLine("Exception: " + e.Message);
                string current = DateTime.Now.ToString("HH:mm:ss");
                StreamWriter logfile = File.AppendText("ZoneEngineLog.txt");
                logfile.WriteLine(current + " " + e.Source + " MySQL Error. Server Cannot Start");
                logfile.WriteLine(current + " " + e.Source + " Exception: " + e.Message);
                logfile.Close();
                zoneServer.Stop();
                ThreadMgr.Stop();
                Process.GetCurrentProcess().Kill();
            }
        }
        public static bool ismodified()
        {
            string[] info = AssemblyInfoclass.Trademark.Split(';');
            return (info[1] == "1");
        }
        public static bool ismixed()
        {
            string[] info = AssemblyInfoclass.Trademark.Split(';');
            return (info[0] == "1");
        }
    }
}
